<template>
    <view class="goods_item">
      <!-- 左侧商品图片 -->
      <view class="goods-item-left">
        <radio :checked="goods.goods_state" color="#c00000" v-if="showRadio" @click="radioClickHandler"></radio>
        <image :src="goods.goods_small_logo || defaulPic" class="goods_pic"></image>
      </view>
      <!-- 右侧商品信息及价格 -->
      <view class="goods-item-right">
        <view class="goods-name">{{goods.goods_name}}</view>
        <view class="goods-info-box">
          <view class="goods-price">￥{{goods.goods_price.toFixed(2)}}</view>
          <uni-number-box :min="1" :value="goods.goods_count" v-if="showNum" @change="numChangeHandler"></uni-number-box>
        </view>
      </view>
    
    </view>
</template>

<script>
  export default {
    name:"my-goods",
    props:{
      // 商品信息
      goods:{
        type: Object,
        default: {}
      },
      showRadio: {
        type: Boolean,
        //默认情况下，不会展示radio勾选状态
        default: false,
      },
      //是否展示价格右侧的NumberBox组件
      showNum: {
        type: Boolean,
        default: false
      },
    },
    data() {
      return {
        //定义默认图片
        defaulPic: 'https://img1.baidu.com/it/u=1841333429,3927409117&fm=253&fmt=auto&app=138&f=JPEG?w=610&h=500'
      };
    },
    methods: {
      //radio点击事件处理函数
      radioClickHandler(){
        this.$emit('radio-change',{
          goods_id: this.goods.goods_id,
          goods_state: !this.goods.goods_state
        })
      },
      numChangeHandler(val){
        this.$emit('num-change',{
          goods_id: this.goods.goods_id,
          goods_count: val - 0
        })
      },
      
    }
    //vue3没有过滤器
    // filters: {
    //   //把数据处理为带两位小数点的数字
    //   tofixed(num){
    //     return Number(num).toFixed(2)
    //   }
    // }
  }
</script>

<style lang="scss">
.goods_item{
     display: flex;
     // width: 750rpx;
     // box-sizing: border-box;
     padding: 10px 5px;
     border: 1px solid #f0f0f0;
     .goods-item-left{  
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-right: 5px;
       .goods_pic{
         width: 100px;
         height: 100px;
        display: block;
       }
     }
     .goods-item-right{
       display: flex;
       flex: 1;
       flex-direction: column;
       // align-items: flex-end;
       justify-content: space-between;
       .goods-name{
         font-size: 13px;
       }
       .goods-info-box{
         display: flex;
         align-items: center;
         justify-content: space-between;
         padding-left: 10px;
         .goods-price{   
           color: #c00000;
           font-size: 16px;
        }
      }
    }
  }
</style>